<template>
  <!-- 双选会二维码统一解析和跳转页面 -->
  <page-wrap :bgColor="dynamicColor" txtColor="#222" :showTop="false" :dark="false" :hideSafeArea="true" hideTab>
    <view class="img-container">
      <image :src="imgUrl" class="img"></image>
      <view class="footer">
        <view :class="['btn']" @click="handleReturn">去体验职途求职工具</view>
      </view>
    </view>
    <LoginDialog ref="loginDialog" @loginSuccessBack="loginSuccessBack"></LoginDialog>
  </page-wrap>
</template>

<script>
import {
  getOneQRInfo,
  cardRegist,
  copyRight,
  copyResume,
  copyCareerAssessment,
  copyZtReport,
  copyAiInterviewRecord,
  getPersonRightNum,
} from "@/api/modules/career";
import { env } from "@/api/root";
import LoginDialog from "@/components/LoginDialog";
export default {
  components: {
    LoginDialog,
  },
  data() {
    return {
      dynamicColor: "transparent",
      navHeight: getApp().globalData.navHeight,
      imgUrl:
        "https://img3.job1001.com/ypMp/elan/images/career/careerPathDesc.jpg",
      personId: uni.getStorageSync("person-id").slice(2) || undefined,
      id: "",
      rightId: "",
      cardType: "",
      valueId: "",
      fromPersonId: "",
      userInfo: "",
      discLeftUseNum: 0,
      careerAssessmentId: "",
    };
  },
  async onLoad(opt) {
    this.showLoading();
    console.log(opt, "opt");
    const { id, rightId } = opt;
    this.id = id;
    this.rightId = rightId;
    console.log(this.id, "this.id");
    console.log(this.rightId, "this.rightId");
    await this.handleBeforeJump();
  },
  onHide() {
    uni.hideLoading();
  },
  methods: {
    handleReturn() {
      uni.reLaunch({ url: "/pagesSub/userCenter/careerPathV2/index" });
    },
    async handleBeforeJump() {
      if (!this.personId) {
        this.showLoginDialog();
        return;
      }
      await this.getOneQRInfoFn();
      const res = await this.cardRegistFn();
      if (res.code == 200) {
        await this.handleCopyRight();
      } else {
        uni.showToast({
          title: res.status_desc,
          icon: "none",
        });
      }
    },
    // 换绑卡
    async cardRegistFn() {
      const params = {
        personId: this.personId,
        cardType: this.cardType,
        force: 1, //  强行绑卡 ，会触发换绑操作,一定成功
      };
      const res = await cardRegist(params);
      return res;
    },
    // 获取二维码信息
    async getOneQRInfoFn() {
      const res = await getOneQRInfo({ id: this.id });
      if (res.code == 200) {
        this.valueId = res.data.valueId;
        this.cardType = res.data.cardType;
        uni.setStorageSync("cardType", this.cardType);
        this.fromPersonId = res.data.fromPersonId;
        this.careerAssessmentId = res.data.careerAssessmentId || "";
      }
    },
    // 复制AI面试记录数据
    async copyAiInterviewRecordFn() {
      const params = {
        fromPersonId: this.fromPersonId,
        rightId: this.rightId,
        valueId: this.valueId,
        personId: this.personId,
        opsShareId: this.id,
      };
      const res = await copyAiInterviewRecord(params);
      this.jumpToPage(
        "/pagesSub/companyCenter/interviewAI/majorSearchAI?fromSxh=fromSxhQrCode"
      ); // 列表页
      if (res.code == 200) {
        // this.jumpToPage(
        //   `/pagesSub/companyCenter/interviewAI/interviewResult?objId=${this.aiResultId}` // 结果详情页
        // );
      } else {
        uni.showToast({
          title: res.status_desc,
          icon: "none",
        });
      }
    },
    // 复制职途报告数据
    async copyZtReportFn() {
      const params = {
        fromPersonId: this.fromPersonId,
        rightId: this.rightId,
        personId: this.personId,
      };
      const res = await copyZtReport(params);
      if (res.code == 200) {
        this.jumpToPage(
          `/pagesSub/userCenter/careerPathV2/components/CreateAndDeliverCareerPathReport?fromSxh=fromSxhQrCode`
        );
      }
    },
    // 复制简历诊断数据
    async copyResumeFn() {
      // const params = {
      //   fromPersonId: this.fromPersonId,
      //   rightId: this.rightId,
      //   personId: this.personId,
      //   resumeAttachmentId: 0
      // };
      // const res = await copyResume(params);
      // if (res.code == 200) {
      // this.jumpToPage('/pagesSub/AIResumeDiagnoseNew/index');
      //   }
      // 简历诊断不需要复制数据过去，所以直接跳
      this.jumpToPage(
        "/pagesSub/AIResumeDiagnoseNew/index?fromSxh=fromSxhQrCode"
      );
    },
    // 复制职业测评数据
    async copyCareerAssessmentFn() {
      const params = {
        fromPersonId: this.fromPersonId,
        rightId: this.rightId,
        personId: this.personId,
        careerAssessmentId: +this.valueId,
      };
      const res = await copyCareerAssessment(params);
      if (res.code == 200) {
        this.jumpToPage(
          `/pagesSub/careerAssessResultActNew/index?personId=${this.personId}&leftUseNum=${this.discLeftUseNum}&fromSxh=fromSxhQrCode&assessmentType=0`
        );
      }
    },
    // 复制性格测评数据
    async copyRightFn() {
      const params = {
        fromPersonId: this.fromPersonId,
        rightId: this.rightId,
        personId: this.personId,
      };
      const res = await copyRight(params);
      if (res.code == 200 && res.data && res.data.extra) {
        const extra = JSON.parse(res.data.extra);
        const userId = extra ? extra.userId : "";
        let url;
        console.log(userId, "useId");
        if (env == "test") {
          url = `https://3gtest.crm1001.com/disc/#/disc/result?userId=${userId}&personId=${this.personId}&iname=${this.personId}&phone=18825449087&from=miniprogramV2&discLeftUseNum=${this.discLeftUseNum}&cardType=${this.cardType}`;
        } else if (env == "pro" || env == "pre") {
          url = `https://3g.crm1001.com/v2/disc/#/disc/result?userId=${userId}&personId=${this.personId}&iname=${this.personId}&phone=18825449087&from=miniprogramV2&discLeftUseNum=${this.discLeftUseNum}&cardType=${this.cardType}`;
        }
        uni.reLaunch({
          url:
            "/pagesSub/public/web/descWebView?link=" + encodeURIComponent(url),
        });
      }
    },
    async getPersonRightNumFn() {
      const params = {
        personId: this.personId || undefined,
        rightId: this.rightId,
        cardType: this.cardType,
      };
      const res = await getPersonRightNum(params);
      if (res.code == 200) {
        this.discLeftUseNum = res.data.ownNum - res.data.useNum;
      }
    },
    async handleCopyRight() {
      switch (+this.rightId) {
        case 1: // 性格测评 1
          await this.getPersonRightNumFn();
          await this.copyRightFn();
          break;
        case 2: // 职业测评 2
          await this.getPersonRightNumFn();
          await this.copyCareerAssessmentFn();
          break;
        case 4: // 简历诊断 4
          await this.copyResumeFn();
          break;
        case 7: // 职途报告 7
          this.copyZtReportFn();
          break;
        case 17: // AI面评  17
          this.copyAiInterviewRecordFn();
          break;
        default:
          break;
      }
    },
    jumpToPage(url) {
      uni.navigateTo({ url });
    },
    showLoginDialog() {
      uni.setStorageSync("isZhituUser", true);
      this.$refs.loginDialog.setVisibility(true);
    },
    loginSuccessBack() {
      this.personId = uni.getStorageSync("person-id").slice(2) || undefined;
      if (this.personId) {
        this.handleBeforeJump();
      }
    },
    showLoading() {
      uni.showLoading({
        title: "请稍后~",
        mask: true,
      });
      setTimeout(() => {
        uni.hideLoading();
      }, 3000);
    },
  },
};
</script>

<style lang="scss" scoped>
.img-container {
  .img {
    width: 100%;
    height: 100vh;
  }
  .footer {
    position: fixed;
    bottom: constant(safe-area-inset-bottom);
    bottom: env(safe-area-inset-bottom);
    left: 32rpx;
    width: calc(100vw - 64rpx);
    display: flex;
    justify-content: space-between;
    align-items: center;
    .btn {
      width: 100%;
      color: #fff;
      font-size: 32rpx;
      text-align: center;
      background: #fe7220;
      border-radius: 50rpx;
    }
  }
}
</style>
